<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import type { FormType } from './index'
import { GetDataDetail, PostCreateData, PutModifyData } from '@/api/modules/vipIcon'
import detailStore from '@/store/modules/cacheData'

const router = useRouter()
const route = useRoute()
const detailData = detailStore()

const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<FormType>({
  title: '',
  linkUrl: '',
  sort: '',
  iconType: '',
  iconPic: '',
  isJump: false,
})

const rules = reactive({
  sort: [
    { required: true, message: '请输入自定义排序', trigger: 'blur' },
  ],
})

const id = route.query.id as string

// 图片上传赋值
function handleUploadSuccess(url: string) {
  if (url) {
    ruleForm.value.iconPic = url
  }
}

// 提交
async function SubmitData() {
  if (id) {
    // 编辑
    PutModifyData(ruleForm.value).then((res: any) => {
      if (res.statusCode === 200) {
        ElMessage.success('编辑成功')
        router.push({ name: 'vipIconIndex' })
      }
    })
  }
  else {
    // 添加
    PostCreateData(ruleForm.value).then((res: any) => {
      if (res.statusCode === 200) {
        ElMessage.success('添加成功')
        detailData.EmptyVipLcon()
        router.push({ name: 'vipIconIndex' })
      }
    })
  }
}

watch(ruleForm, (newValue) => {
  if (!id) {
    detailData.RememberVipLcon(newValue)
  }
}, { deep: true })

onMounted(async () => {
  if (id) {
    await GetDataDetail(id).then((res: any) => {
      if (res.statusCode === 200) {
        ruleForm.value = res.data
      }
    })
  }
  else {
    ruleForm.value = detailData.GetVipLconData()
  }
})
</script>

<template>
  <div>
    <PageHeader :title="id ? '编辑' : '添加'" />
    <ElForm ref="ruleFormRef" label-position="top" label-width="80px" :rules="rules" :model="ruleForm">
      <PageMain title="基础信息">
        <ElRow :gutter="20">
          <ElCol :md="8">
            <ElFormItem label="标题（不超过6个字）">
              <el-input v-model="ruleForm.title" style="width: 300px;" maxlength="6" placeholder="请输入标题" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="跳转链接">
              <el-input v-model="ruleForm.linkUrl" style="width: 300px;" placeholder="请输入跳转链接" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="自定义排序（升序）" prop="sort">
              <el-input v-model="ruleForm.sort" style="width: 300px;" placeholder="请输入自定义排序" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="类型">
              <el-select v-model="ruleForm.iconType" placeholder="请选择" style="width: 300px;">
                <el-option label="会员攻略" :value="1" />
                <el-option label="会员权益" :value="2" />
              </el-select>
            </ElFormItem>
          </ElCol>
          <ElCol :md="8">
            <ElFormItem label="是否允许跳转">
              <el-switch v-model="ruleForm.isJump" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="24">
            <ElFormItem label="奖品图">
              <ImageUpload
                width="auto" height="200" :size="5" :url="ruleForm.iconPic" :ext="['jpg', 'png']"
                @on-success="(imgUrl: string) => handleUploadSuccess(imgUrl)"
              />
            </ElFormItem>
          </ElCol>
        </ElRow>
      </PageMain>
    </ElForm>
    <div style="height: 80px;" />
    <FixedActionBar>
      <ElButton type="primary" size="large" @click="SubmitData()">
        提交
      </ElButton>
    </FixedActionBar>
  </div>
</template>

<style lang="scss" scoped>
.box {
  .title {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}

.card-box {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 10px;
}
</style>
